import java.util.*;

/**
 * @author wangxi created on 2021/3/16 22:28
 * @version v1.0
 */
public class Solution {

    public static void main(String[] args) {
        Solution obj = new Solution();
        int[][] ints = obj.generateMatrix(3);
        System.out.println(Arrays.toString(ints));
    }

    int num = 1;

    public int[][] generateMatrix(int n) {
        if (n <= 0) {
            return new int[0][0];
        }
        int[][] result = new int[n][n];
        for (int i = 0; 2 * i < n; i++) {
            fill(result, i, n);
        }
        return result;
    }

    private void fill(int[][] result, int start, int n) {
        int endRow = n - start - 1;
        int endCol = n - start - 1;
        for (int i = start; i <= endCol; i++) {
            result[start][i] = num++;
        }
        // 需要两行
        if (endRow > start) {
            for (int i = start + 1; i <= endRow; i++) {
                result[endCol][i] = num++;
            }
        }
        // 需要两行两列
        if (endRow > start && endCol > start) {
            for (int i = endCol - 1; i >= start; i--) {
                result[endRow][i] = num++;
            }
        }
        if (endRow > start + 1 && endCol > start) {
            for (int i = endRow - 1; i > start; i--) {
                result[i][start] = num++;
            }
        }
    }
}

